import axios from "axios";
import {message} from "antd";
import {getToken} from "../utils/user-token";

const instance = axios.create({
  timeout: 10 * 1000
})
instance.interceptors.request.use(config => {
  console.log('getToken', getToken())
  config.headers['Authorization'] = `Bearer ${getToken()}`
  return config
}, error => {
  return Promise.reject(error)
})

// 相应拦截器
instance.interceptors.response.use(res => {
  const resData = (res.data || {}) as Restype
  const {errno, msg, data} = resData
  if (errno !== 0) {
    if (msg) {
      message.error(msg)
    }
    throw new Error(msg)
  }
  return data as any
})
export type Restype = {
  errno: number,
  msg?: string,
  data?: ResDataType
}
export type ResDataType = {
  [key: string]: any
}

export default instance
